package ch3.ex9;

import static ch3.print.Print.*;

import java.util.*;

public class Fibonacci {
	static long s_i;
	static long s_ii;

	long fib(long i) {
		if (i < 2)
			return 1;
		return (fib(i - 2) + fib(i - 1));
	}

	long my_fib(int i) {
		
		switch (i) {
		case 0:
		case 1:
			s_i = 1;
			break;
		case 2:
			s_ii = 2;
			break;
		default:
			if (s_ii > s_i)
				s_i +=s_ii;
			else
				s_ii += s_i;
			
		}
		return (s_ii > s_i) ? s_ii : s_i;
//		if (i < 2) {
//			s_i = 1;
//			return 1;
//		}
//		if (i == 2)	s_ii = 2;
//		if (i > 2)
//			if (s_ii > s_i)
//				s_i +=s_ii;
//			else
//				s_ii += s_i;
//
//		return (s_ii > s_i) ? s_ii : s_i;
	}

	public static void main(String[] args) {
		Fibonacci f = new Fibonacci();
		// int k = Integer.parseInt(args[0]);
		long k = 40;
		println("First " + k + " fibonacci numbers: ");
		long t1 = System.nanoTime();
		for (int i = 0; i < k; i++) {
			println(i+ " - " + f.fib(i));
		}
		long t2 = System.nanoTime();
		long t3 = System.nanoTime();
		for (int i = 0; i < k; i++) {
			println(i+ " - " + f.my_fib(i));
		}
		long t4 = System.nanoTime();
		
		println("Time fib() " + (t2-t1) + " " + (((t4-t3)>(t2-t1))?">":"<") + " the time my_fib()" + (t4-t3) + ".");
		println("More than on " + (((t4-t3)>(t2-t1))?((t4-t3) - (t2-t1)):((t2-t1)-(t4-t3))) + " nanoseconds.");
	}
}
